
package com.ifcar.app.activity.common;

import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

/**
 * 回复工具条通用类
 * 
 * @author wjying
 */
public class CommonReply implements OnClickListener, OnFocusChangeListener {
    public static final int ACTION_SHARE = 0;

    public static final int ACTION_COLLECTED = ACTION_SHARE + 1;

    public static final int ACTION_UNCOLLECTED = ACTION_COLLECTED + 1;

    public static final int ACTION_REPLY = ACTION_UNCOLLECTED + 1;

    private Activity mActivity;

    private View mContainer;

    private View mReplyEditLayout;

    private View mReplyImgLayout;

    private EditText mReplyEdittext;

    private Button mReplyButton;

    private CheckBox mCollectButton;

    private View mShareButton;

    private View mReplyEditButton;

    private ActionCallback mCallback;

    private Object mEditTag;

    private boolean mReady;
    
    private boolean mCollectEnable = true;

    public CommonReply(Activity activity, View container) {
        mActivity = activity;
        mContainer = container;

        mReplyEditLayout = container.findViewById(R.id.reply_edittext_layout);
        mReplyImgLayout = container.findViewById(R.id.reply_img_layout);

        mReplyEdittext = (EditText) mReplyEditLayout.findViewById(R.id.reply_edittext);
        mReplyEdittext.setOnFocusChangeListener(this);
        mReplyButton = (Button) mReplyEditLayout.findViewById(R.id.reply_button);
        mReplyButton.setOnClickListener(this);

        mCollectButton = (CheckBox) mReplyImgLayout.findViewById(R.id.collect_button);
        mCollectButton.setOnClickListener(this);
        mShareButton = mReplyImgLayout.findViewById(R.id.share_button);
        mShareButton.setOnClickListener(this);
        mReplyEditButton = mReplyImgLayout.findViewById(R.id.reply_img_button);
        mReplyEditButton.setOnClickListener(this);
        
        setClickViewEnable(false);
    }

    public void setCallback(ActionCallback callback) {
        mCallback = callback;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        final int keyCode = event.getKeyCode();
        final int action = event.getAction();

        if (mReplyEditLayout.getVisibility() == View.VISIBLE && checkDismissKey(keyCode)) {
            if (action == KeyEvent.ACTION_UP) {
                finishEdit();
            }
            return true;
        }
        return false;
    }

    protected boolean checkDismissKey(int keyCode) {
        return keyCode == KeyEvent.KEYCODE_BACK;
    }

    public void ready() {
        mReady = true;
        setClickViewEnable(true);
    }
    
    public void setClickViewEnable(boolean enabled) {
        mReplyEdittext.setEnabled(enabled);
        mReplyButton.setEnabled(enabled);
        mCollectButton.setEnabled(enabled && mCollectEnable);
        mShareButton.setEnabled(enabled);
        mReplyEditButton.setEnabled(enabled);
    }
    
    public void setCollectEnabled(boolean enabled) {
        mCollectEnable = enabled;
        mCollectButton.setEnabled(enabled);
    }
    
    public void setCollectVisiabled(boolean visiabled) {
        mCollectButton.setVisibility(visiabled ? View.VISIBLE : View.GONE);
        View devider = mContainer.findViewById(R.id.reply_frame_divider_img2);
        devider.setVisibility(visiabled ? View.VISIBLE : View.GONE);
    }
    
    @Override
    public void onClick(View v) {
        if (!mReady) {
            return;
        }
        int id = v.getId();
        if (dispatchClick(id)) {
            return;
        }
    }

    public boolean dispatchClick(int id) {
        boolean ret = true;
        switch (id) {
            case R.id.collect_button:
                callCallback(mCollectButton.isChecked() ? ACTION_COLLECTED : ACTION_UNCOLLECTED);
                break;
            case R.id.reply_img_button:
                beginEdit(null);
                break;
            case R.id.reply_button:
                callCallback(ACTION_REPLY);
                mReplyEdittext.setText("");
                break;
            case R.id.share_button:
                callCallback(ACTION_SHARE);
                break;
            default:
                ret = false;
                break;
        }
        return ret;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            finishEdit();
        }

    }

    public void beginEdit(Object editTag) {
        mEditTag = editTag;
        mReplyEdittext.requestFocus();

        InputMethodManager imm = (InputMethodManager) mActivity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(mReplyEdittext, 0);
        }
        
        mReplyEditLayout.setVisibility(View.VISIBLE);
        mReplyImgLayout.setVisibility(View.GONE);
    }

    public void finishEdit() {
        mEditTag = null;
        mReplyEditLayout.setVisibility(View.GONE);
        mReplyImgLayout.setVisibility(View.VISIBLE);
//        mReplyEdittext.setText("");

        InputMethodManager imm = (InputMethodManager) mActivity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mReplyEdittext.getWindowToken(), 0);
    }

    public void setCollected(boolean collected) {
        mCollectButton.setChecked(collected);
    }

    public String getReplyContent() {
        return mReplyEdittext.getText().toString();
    }

    private void callCallback(int action) {
        if (mCallback != null) {
            mCallback.onReplyAction(this, action);
        }
    }

    public Object getEditTag() {
        return mEditTag;
    }

    public interface ActionCallback {
        public void onReplyAction(CommonReply commonReply, int action);
    }

    public void onApplyTheme(Context context, ThemeSettingsHelper themeSettingsHelper) {
        themeSettingsHelper.setViewBackgroud(context, mContainer, R.drawable.reply_frame_bk);

        themeSettingsHelper.setTextViewColor(context, mReplyButton, R.color.list_title_color);
        themeSettingsHelper.setViewBackgroud(context, mReplyButton, R.drawable.netease_big_button);

        themeSettingsHelper.setViewBackgroud(context, mCollectButton,
                R.drawable.collect_button_selector);
        themeSettingsHelper.setImageViewSrc(context, (ImageView) mShareButton,
                R.drawable.share_button_selector);
        themeSettingsHelper.setImageViewSrc(context, (ImageView) mReplyEditButton,
                R.drawable.reply_editbox_bk);

        themeSettingsHelper.setImageViewSrc(context,
                (ImageView) mContainer.findViewById(R.id.reply_frame_divider_img0),
                R.drawable.reply_frame_divider);
        themeSettingsHelper.setImageViewSrc(context,
                (ImageView) mContainer.findViewById(R.id.reply_frame_divider_img1),
                R.drawable.reply_frame_divider);
        themeSettingsHelper.setImageViewSrc(context,
                (ImageView) mContainer.findViewById(R.id.reply_frame_divider_img2),
                R.drawable.reply_frame_divider);

    }
}
